/*
* @file app.cpp
* @brief This file contains the implementation of the App class
* @author feiniao
* @version 1.0
* upda
* @date 2025-5-11
* @note This code is designed to work with ESP32-S3 and uses the ESP-IDF 5.2.3 framework.
*       It provides a main application class that handles WiFi connection and STT processing.
*/


#include "app.h"

TaskHandle_t App::wifi_task_handle = nullptr;       // wifi task handle
bool task_end_flag = true;

/*
* @brief App class constructor
* @note This function initializes the App object.
* It creates instances of MyWifi, STT, and BSP_KEY classes.
* The constructor is called when the App object is created.
*/
App::App() : wifi(), stt(), llm(), tts(), key(GPIO_NUM_10), gm() {
    ESP_LOGI(TAG, "App对象创建成功!");
}


/*
* @brief App class destructor
* @note This function is called when the App object is destroyed.
*/
App::~App() {
    ESP_LOGI(TAG, "App对象销毁成功!");
}


/*
* @brief WiFi task function
* @param arg Pointer to user data
* @note This function is responsible for connecting to the WiFi network.
* It creates a task that continuously checks the WiFi connection status.
* When the WiFi is connected, it creates a task for STT, LLM, TTS processing.
*/
void App::wifi_task(void* arg) {
    App* app = static_cast<App*>(arg);
    app->wifi.connect_wifi();
    
    while (1) {
        wifi_ap_record_t ap_info;
        if (esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK) {
            ESP_LOGI(TAG, "WIFI已连接,准备启动STT任务");
            xTaskCreatePinnedToCore(
                stt_task, 
                "stt_task", 
                4096, 
                app,    // 静态函数传不了this, 传app
                5,
                nullptr, 
                0
            );
            xTaskCreatePinnedToCore(
                llm_task, 
                "llm_task", 
                4096, 
                app,    // 静态函数传不了this, 传app
                5, 
                nullptr, 
                0
            );
            xTaskCreatePinnedToCore(
                tts_task, 
                "tts_task", 
                4096, 
                app,    // 静态函数传不了this, 传app
                5, 
                nullptr, 
                0
            );
            break;
        }
        vTaskDelay(pdMS_TO_TICKS(100)); // 添加延迟避免忙等待
    }
    
    vTaskDelete(nullptr); // 任务完成后删除自己
}


/*
* @brief STT task function
* @param arg Pointer to user data
* @note This function is responsible for processing the STT (Speech-to-Text) functionality.
* It creates a task that continuously checks the key press status.
* When the key is pressed, it calls the STT process function.
*/
void App::stt_task(void* arg) {
    ESP_LOGI(TAG, "STT任务已启动");
    App* app = static_cast<App*>(arg);

    while (1) {
        if (task_end_flag) {
            // ESP_LOGI(TAG, "task_end_flag: %d", task_end_flag);      // 调试信息
            // 检查WiFi连接状态
            wifi_ap_record_t ap_info;
            bool wifi_connected = (esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK);
            if (wifi_connected) {
                if (app->key.get_key_num() == GPIO_NUM_10) {
                    app->stt.process_int16t();
                    task_end_flag = false;
                }
            } else {
                ESP_LOGW(TAG, "WIFI未连接, 无法进行STT");
            }
        }
        vTaskDelay(pdMS_TO_TICKS(10));
    }
}


void App::llm_task(void* arg) {
    ESP_LOGI(TAG, "LLM任务已启动");

    App* app = static_cast<App*>(arg);

    while (1) {
        wifi_ap_record_t ap_info;
        if (esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK) {
            app->llm.process();
        } else {
            ESP_LOGW(TAG, "WIFI未连接, 无法进行LLM");
            vTaskDelay(pdMS_TO_TICKS(2000)); // 添加延迟避免忙等待
        }
        vTaskDelay(pdMS_TO_TICKS(10)); // 添加延迟避免忙等待
    }
}


void App::tts_task(void* arg) {
    ESP_LOGI(TAG, "TTS任务已启动");

    App* app = static_cast<App*>(arg);

    while (1) {
        wifi_ap_record_t ap_info;
        if (esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK) {
            app->tts.process();
            task_end_flag = true;
        } else {
            ESP_LOGW(TAG, "WIFI未连接, 无法进行TTS");
            vTaskDelay(pdMS_TO_TICKS(2000)); // 添加延迟避免忙等待
        }
        vTaskDelay(pdMS_TO_TICKS(10)); // 添加延迟避免忙等待
    }
}

/*
* @brief Main function to run the App
* @note This function creates a task for WiFi connection and starts the main loop.
* It continuously checks the WiFi connection status and runs the main task loop.
*/
void App::run() {
    xTaskCreatePinnedToCore(
        wifi_task, 
        "wifi_task", 
        4096, 
        this,  // 传递this指针
        5, 
        &wifi_task_handle, 
        0
    );
    
    while (1) {
        vTaskDelay(pdMS_TO_TICKS(1000)); // 主任务循环
    }
}
